package com.ygj.shop.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import javax.annotation.Resource;

import com.base.common.ConfigConstants;
import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.datesource.MultipleDataSource;
import com.base.common.persistent.query.TNewsQuery;
import com.cjy.common.bean.OpAttributes;
import com.ygj.shop.service.NewsService;
import org.apache.commons.lang3.StringUtils;


import static com.base.common.GlobaConstants.*;

@Action(value = "newsAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class NewsAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private NewsService newsService;

	private int page;
	
	private int cid;
	
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public String getNewsList() {
		try {
			TNewsQuery newsQuery = new TNewsQuery();
			newsQuery.setCtypeB(cid);
			newsQuery.setCtype("cp");
			newsQuery.setOffset((page-1) * Integer.valueOf(StringUtils.strip(ConfigConstants.INFOS_LIST_NUM)));
			MultipleDataSource.setDataSourceKey("shopDataSource");
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 1).add("result", newsService.getNewsByCondition(newsQuery)));
			MultipleDataSource.setDataSourceKey("sqlserverDataSource");
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}
	
	
	public String getNewsListCounts() {
		try {
			TNewsQuery newsQuery = new TNewsQuery();
			newsQuery.setCtypeB(cid);
			newsQuery.setCtype("cp");
			MultipleDataSource.setDataSourceKey("shopDataSource");
				actionResult = ActionResult.getActionResult(new OpAttributes().add(
						"code", 1).add("counts", newsService.getNewsByConditionCount(newsQuery)));
				MultipleDataSource.setDataSourceKey("sqlserverDataSource");

		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}
}
